from . import view
from . import schema
from common import cache
from typing import List
from fastapi import APIRouter, Security
from package.fastapi import permission


router_lock = APIRouter(prefix='/lock', tags=['临时锁定'])
router_meta = {'module': '系统管理', 'sub_module': '临时锁定'}


@router_lock.get('/user', response_model=List[str],
                 openapi_extra=router_meta, name='lock_user_get', summary='临锁用户获取')
def lock_user_get(user=Security(permission.security, scopes=['lock_user:read'])):
    return cache.temp_userlock_cache.get()


@router_lock.delete('/user', response_model=schema.List[str],
                    openapi_extra=router_meta, name='lock_user_unlock', summary='临锁用户解锁')
def lock_user_unlock(body: schema.UserBatch, user=Security(permission.security, scopes=['lock_user:write'])):
    for user in body.user:
        cache.temp_userlock_cache.delete(user)
    return cache.temp_userlock_cache.get()


@router_lock.get('/ip', response_model=schema.List[str],
                 openapi_extra=router_meta, name='lock_ip_get', summary='临锁IP获取')
def lock_ip_get(user=Security(permission.security, scopes=['lock_ip:read'])):
    return cache.temp_iplock_cache.get()


@router_lock.delete('/ip', response_model=schema.List[str],
                    openapi_extra=router_meta, name='lock_ip_unlock', summary='临锁IP解锁')
def lock_ip_unlock(body: schema.IPBatch, user=Security(permission.security, scopes=['lock_ip:write'])):
    for ip in body.ip:
        cache.temp_iplock_cache.delete(ip)
    return cache.temp_iplock_cache.get()
